<?php
namespace app\modules\backend\controllers;

use app\models\OrderInfo;
use app\tools\Refund;
use app\tools\Re;
use Yii;

class FinanceController extends BaseController
{
    public function actionFinanceInfo() {
      $date_range = Yii::$app->request->post('created_at_range', '');
      $money_all_query = OrderInfo::find()->where(['order_type' => 1, 'state' => [1,2,3,4,5,6,7]]);
      $money_djs_query = OrderInfo::find()->where(['order_type' => 1, 'state' => [1,2,3,5,7]]);
      $money_yjs_query = OrderInfo::find()->where(['order_type' => 1, 'state' => 4]);
      $money_tk_query = OrderInfo::find()->where(['order_type' => 1, 'state' => 6]);
      if ($date_range) {
        $data_arr = explode("-", $date_range);
        $start_at = $data_arr[0];
        $end_at = $data_arr[1]+86400;
        $money_all_query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
        $money_djs_query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
        $money_yjs_query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
        $money_tk_query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
      }

      $money_all = $money_all_query->SUM('money');//系统交易总金额
      $money_djs = $money_djs_query->SUM('money');//待结算总金额
      $money_yjs = $money_yjs_query->SUM('money');//已结算总金额
      $money_tk = $money_tk_query->SUM('money');//退款成功总金额

      $money_all = is_numeric($money_all) ? $money_all : 0;
      $money_djs = is_numeric($money_djs) ? $money_djs : 0;
      $money_yjs = is_numeric($money_yjs) ? $money_dj : 0;
      $money_tk = is_numeric($money_tk) ? $money_dj : 0;

      $data = [
        'money_all' => floatval($money_all),
        'money_djs' => floatval($money_djs),
        'money_yjs' => floatval($money_yjs),
        'money_tk' => floatval($money_tk),
      ];
      return Re::data(['data' => $data]);
    }

    //账单明细
  	public function actionOrderRecord() {
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 20);
      $date_range = Yii::$app->request->post('created_at_range', '');
      $order_query = OrderInfo::find()
        ->where("(order_type = 1 and state > 0 and state <> 8) OR (order_type = 2 and refund_state = 1)");
      if ($date_range) {
        $data_arr = explode("-", $date_range);
        $start_at = $data_arr[0];
        $end_at = $data_arr[1]+86400;
        $order_query->andWhere("created_at >= '$start_at' and created_at <= '$end_at'");
      }
      $order_list = $order_query->orderBy('created_at desc')->limit($page_size)->offset($page_size*$page)->all();
      $order_data = [];
      if ($order_list) {
        foreach ($order_list as $v) {
          $order_data[] = [
            'id' => $v->id,
            'name' => $v->user->name,
            'phone' => $v->user->phone,
            'order_sn' => $v->order_sn,
            'created_time' => date("Y-m-d H:i", $v->created_at),
            'remark' => $v->remark,
            'money' => floatval($v->money),
            'order_type' => $v->order_type,
            'moneys' => ($v->order_type == 1 ? '+' : '-') . floatval($v->money)
          ];
        }
      }
  		return Re::data([
        'data' => $order_data,
        'totalPage' => $order_query->count(),
        'currentPage' => $page,
        'perPage' => OrderInfo::PAGE_SIZE
      ]);
  	}

    //编辑账单明细
    public function actionEditRemark() {
      $id = Yii::$app->request->post('id', 0);
      $remark = Yii::$app->request->post('remark', '');
      $order = OrderInfo::findOne($id);
      if (!$order) {
        return Re::data(null, '账单信息读取失败', Re::ERROR);
      }
      $order->remark = $remark;
      if (!$order->save()) {
        return Re::data(null, '操失失败', Re::ERROR);
      }
      return Re::data();
    }
}
